import Vue from 'vue'
import Vuex from 'vuex'
import { loginApi } from '@/api'
// import { getToken, setToken } from '@/utils/ls'
import { getToken, removeToken, setToken } from '@/utils/auth'
Vue.use(Vuex)

export default new Vuex.Store({
  state: {
    token: getToken() || ''
  },
  getters: {
  },
  mutations: {
    // token 持久化 一式两份  vuex 一份  本地一份
    setToken (state, token) {
      state.token = token
      setToken(token)
    },
    // 清除数据的方法
    removeToken (state) {
      state.token = ''
      removeToken()
    }
  },
  actions: {
    async fetchLogin ({ commit }, form) {
      const { data } = await loginApi(form)
      // console.log(data)
      commit('setToken', data.token)
    }
  },
  modules: {
  }
})
